<template>
  <van-button
    :icon="value ? 'star' : 'star-o'"
    :class="{ collect: value }"
    @click="onCollect"
    :loading="loading"
  >
  </van-button>
</template>

<script>
import { addCollect, deleteCollect } from '../../api/article'
export default {
  name: 'collectArticle',
  components: {},
  props: {
    value: {
      type: Boolean,
      required: true
    },
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data() {
    return {
      loading: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onCollect() {
      this.loading = true
      try {
        if (this.value) {
          // 已收藏 取消收藏
          await deleteCollect(this.articleId)
        } else {
          //    收藏
          await addCollect(this.articleId)
        }
        this.$emit('input', !this.value)
        this.$toast.success(this.value ? '取消收藏' : '成功收藏')
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败 请稍后重试')
      }
      this.loading = false
    }
  }
}
</script>
<style lang="less" scoped>
.collect {
  .van-icon {
    color: yellow;
  }
}
.noboder {
  border: none;
}
</style>
